package com.rd.student.ruanjian.class3.gaochenxi.BiJiao;

import java.util.*;

public class TestMap {

   public static void main(String[] args) {
      //遍历key集
      HashMap map=new HashMap();
      //添加：put(key,value)
      map.put("A",56);
      map.put(67,"Tom");
      map.put("CC",78);
      map.put("9","re");
      Set keySet= map.keySet();
      //使用迭代器
      Iterator iterator = keySet.iterator();
      while(iterator.hasNext()){
         System.out.print(iterator.next()+" ");//CC A 67 9
      }
      System.out.println();
      System.out.println("===========================");
      //遍历value:Collection values()
      //方式1：直接获取values
      Collection collection = map.values();
      for(Object obj:collection){
         System.out.print(obj+" ");//78 56 Tom re
      }
      System.out.println();
      //方式2：通过key来获取values
      Set keySet2 = map.keySet();
      for(Object obj2:keySet2){
         Object value=map.get(obj2);
         System.out.print(value+" ");
      }
      System.out.println();
      System.out.println("===========================");

      //遍历Entry集合来调用getKey()方法获取键，通过调用getValue()方法来获取值
      Set set = map.entrySet();
      Iterator iterator1 = set.iterator();
      while(iterator1.hasNext()){
         Map.Entry entry = (Map.Entry) iterator1.next();
         System.out.println(entry.getKey()+" "+entry.getValue());
      }
   }
//   public static void Func(HashMap hashMap){
//      Set set=hashMap.keySet();
//      Iterator iterator = set.iterator();
//      while(iterator.hasNext()){
//         Object key=iterator.next();
//         Object value=hashMap.get(key);
//         System.out.println(key+" "+value);
//      }
//   }

}
